﻿#region 命名空间

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using WinnerCMS.BLL;
using WinnerCMS.Common;
using WinnerCMS.Model;
using WinnerCMS.Model.Attribute;

#endregion

namespace WinnerCMS.WebUI.Admin.UDT
{
    [Access("262001,262003")]
    public partial class Add : WebFactory.PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.BC_Path.Items.Add(new ListItem {Text = "标签管理", Value = "UDT_Manage.aspx"});
            if (this.ddlType.SelectedValue == "0")
            {
                this.BC_Path.Items.Add(new ListItem {Text = "超级标签"});
                Title = "超级标签";
            }
            if (this.ddlType.SelectedValue == "1")
            {
                this.BC_Path.Items.Add(new ListItem {Text = "Ajax标签"});
                Title = "Ajax标签";
            }
            if (this.ddlType.SelectedValue == "2")
            {
                this.BC_Path.Items.Add(new ListItem {Text = "Js调用标签"});
                Title = "Js调用标签";
            }

            if (!IsPostBack)
            {
                this.Wizard1.ActiveStepIndex = 0;


                if (Request["ID"] != null)
                {
                    Edit_Load();
                }
                else
                {
                    this.ddlType.SelectedValue = Request["Type"];

                    if (this.ddlType.SelectedValue == "2")
                    {
                        this.BC_Path.Items.Add(new ListItem {Text = "Js调用标签"});
                        Title = "Js调用标签";
                        this.trJsPath.Visible = true;
                    }
                    else
                    {
                        this.trJsPath.Visible = false;
                    }
                }
            }
        }

        #region 添加或修改

        private void AddUI()
        {
            var model = new WinnerCMS.Model.UDT
            {
                Name = this.txtName.Text.Trim(),
                DataSource = this.txtDataSource.Text.Trim(),
                SqlScript = this.txtSqlScript.Text.Trim(),
                Type = int.Parse(this.ddlType.SelectedValue),
                JsPath = this.txtJsPath.Text.Trim(),
                URLType = this.dllURLType.SelectedValue,
                FirstItemTemplate = this.txtFirstItemTemplate.Text.Trim(),
                SecondItemTemplate = this.txtSecondItemTemplate.Text.Trim(),
                AlternatingItemTemplate = this.txtAlternatingItemTemplate.Text.Trim(),
                SeparatorTemplate = this.txtSeparatorTemplate.Text.Trim(),
                ItemTemplate = this.txtItemTemplate.Text.Trim(),
                HeaderTemplate = this.txtHeaderTemplate.Text.Trim(),
                FooterTemplate = this.txtFooterTemplate.Text.Trim(),
                EmptyDataTemplate = this.txtEmptyTemplate.Text.Trim(),
                Remark = this.txtRemark.Text.Trim(),
                Tag = this.txtTag.Text.Trim()
            };

            if (Request["ID"] == null)
            {
                model.Id = 0;
            }
            else
            {
                model.Id = int.Parse(Request["ID"]);
            }
            var res = new UDTManage().Add(model);

            if (string.IsNullOrEmpty(res.Errors) != true)
            {
                MsgBox.Message(res.Errors, this.UpdatePanel1);
            }
            else
            {
                MsgBox.Message("录入成功！", this.UpdatePanel1);
            }
        }

        #endregion

        #region 加载

        private void Edit_Load()
        {
            var model = new UDTManage().GetInfo(Request["ID"]);
            if (model != null)
            {
                if (model.Type == 3)
                {
                    Response.Redirect("UDT_JS.aspx?ID=" + Request["ID"]);
                }
                this.txtName.Text = model.Name;
                this.txtDataSource.Text = model.DataSource;
                this.txtSqlScript.Text = model.SqlScript;

                this.txtFirstItemTemplate.Text = model.FirstItemTemplate;
                this.txtSecondItemTemplate.Text = model.SecondItemTemplate;
                this.txtAlternatingItemTemplate.Text = model.AlternatingItemTemplate;
                this.txtSeparatorTemplate.Text = model.SeparatorTemplate;
                this.txtItemTemplate.Text = model.ItemTemplate;
                this.txtHeaderTemplate.Text = model.HeaderTemplate;
                this.txtFooterTemplate.Text = model.FooterTemplate;


                this.txtRemark.Text = model.Remark;
                this.dllURLType.SelectedValue = model.URLType;
                this.txtTag.Text = model.Tag;
                this.ddlType.SelectedValue = model.Type.ToString();
                this.txtJsPath.Text = model.JsPath;


                Title = "修改" + model.Name;

                this.trJsPath.Visible = this.ddlType.SelectedValue == "2";

                this.BC_Path.Items.Add(new ListItem {Text = "修改" + model.Name});
            }
            else
            {
                //转向错误页面
                ErrClass.Redirect("数据不存在。");
            }
        }

        #endregion

        protected void Wizard1_NextButtonClick(object sender, WizardNavigationEventArgs e)
        {
            string strErr = null;
            switch (e.CurrentStepIndex)
            {
                case 0:
                    if (this.txtName.Text.Trim().Length == 0)
                    {
                        strErr = "输入标签名称";
                    }
                    if (Request["Type"] == "2" && this.txtJsPath.Text.Trim().Length == 0)
                    {
                        strErr = "请输入Js保存的路径";
                    }
                    break;
                case 1:

                    if (this.txtSqlScript.Text.Trim().Length == 0)
                    {
                        strErr = "输入Sql代码";
                    }
                    break;
                case 2:
                    if (this.txtItemTemplate.Text.Trim().Length == 0)
                    {
                        strErr = "循环项模板";
                    }
                    break;
            }

            if (strErr != null)
            {
                MsgBox.Message(strErr, this.UpdatePanel1);
                e.Cancel = true;
            }
            else
            {
                switch (e.CurrentStepIndex)
                {
                    case 2:
                        this.txtTiShi.Text = string.Format("<TW:WinnerCMS ID=\"{0}\"></TW:WinnerCMS>", this.txtName.Text);

                        if (this.txtItemTemplate.Text.Trim().Length > 0)
                        {
                            var sbItemTemplate = new StringBuilder();
                            sbItemTemplate.AppendLine("<TW:DataBox runat=\"Server\">\r\n");
                            if (this.txtFirstItemTemplate.Text.Trim().Length > 0)
                            {
                                sbItemTemplate.AppendLine("<FirstItemTemplate>");
                                sbItemTemplate.AppendLine(this.txtFirstItemTemplate.Text.Trim());
                                sbItemTemplate.AppendLine("</FirstItemTemplate>");
                            }
                            if (this.txtSecondItemTemplate.Text.Trim().Length > 0)
                            {
                                sbItemTemplate.AppendLine("<SecondItemTemplate>");
                                sbItemTemplate.AppendLine(this.txtSecondItemTemplate.Text.Trim());
                                sbItemTemplate.AppendLine("</SecondItemTemplate>");
                            }
                            if (this.txtAlternatingItemTemplate.Text.Trim().Length > 0)
                            {
                                sbItemTemplate.AppendLine("<AlternatingItemTemplate>");
                                sbItemTemplate.AppendLine(this.txtAlternatingItemTemplate.Text.Trim());
                                sbItemTemplate.AppendLine("</AlternatingItemTemplate>");
                            }
                            if (this.txtSeparatorTemplate.Text.Trim().Length > 0)
                            {
                                sbItemTemplate.AppendLine("<SeparatorTemplate>");
                                sbItemTemplate.AppendLine(this.txtSeparatorTemplate.Text.Trim());
                                sbItemTemplate.AppendLine("</SeparatorTemplate>");
                            }
                            if (this.txtItemTemplate.Text.Trim().Length > 0)
                            {
                                sbItemTemplate.AppendLine("<ItemTemplate>");
                                sbItemTemplate.AppendLine(this.txtItemTemplate.Text.Trim());
                                sbItemTemplate.AppendLine("</ItemTemplate>");
                            }

                            if (this.txtEmptyTemplate.Text.Trim().Length > 0)
                            {
                                sbItemTemplate.AppendLine("<EmptyTemplate>");
                                sbItemTemplate.AppendLine(this.txtEmptyTemplate.Text.Trim());
                                sbItemTemplate.AppendLine("</EmptyTemplate>");
                            }
                            if (this.txtHeaderTemplate.Text.Trim().Length > 0)
                            {
                                sbItemTemplate.AppendLine("<HeaderTemplate>");
                                sbItemTemplate.AppendLine(this.txtHeaderTemplate.Text.Trim());
                                sbItemTemplate.AppendLine("</HeaderTemplate>");
                            }
                            if (this.txtFooterTemplate.Text.Trim().Length > 0)
                            {
                                sbItemTemplate.AppendLine("<FooterTemplate>");
                                sbItemTemplate.AppendLine(this.txtFooterTemplate.Text.Trim());
                                sbItemTemplate.AppendLine("</FooterTemplate>");
                            }
                            sbItemTemplate.AppendLine("<SqlTemplate>");
                            sbItemTemplate.AppendLine(this.txtSqlScript.Text.Trim());
                            sbItemTemplate.AppendLine("</SqlTemplate>");
                            sbItemTemplate.AppendLine("</TW:UDT>");
                            this.txtTag.Text = sbItemTemplate.ToString();
                        }
                        break;
                }
            }
        }

        protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
        {
            AddUI();

            if (this.ddlType.SelectedValue == "2")
            {
                new BLL.PageFactory.Pages.UDT().SaveJs(this.txtJsPath.Text.Trim(), this.txtTag.Text.Trim());
            }
        }
    }
}